package com.rc.saas.tenant.service.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.redis.RedisLoader;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.utils.DateHelper;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.log.OrderTraceVo;
import com.rc.saas.tenant.mapper.operator.TenantOperatorMapper;
import com.rc.saas.tenant.mapper.order.OrderInfoMapper;
import com.rc.saas.tenant.model.elec.ElecStationPrice;
import com.rc.saas.tenant.model.enterprise.EnterpriseMember;
import com.rc.saas.tenant.model.enterprise.EnterpriseMemberExample;
import com.rc.saas.tenant.model.hlht.HlhtOperator;
import com.rc.saas.tenant.model.operator.TenantOperatorExample;
import com.rc.saas.tenant.model.order.HlhtOrderInfo;
import com.rc.saas.tenant.search.enterprise.EnterpriseOrderSearch;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.model.order.OrderInfoExample;
import com.rc.saas.tenant.search.order.*;
import com.rc.saas.tenant.search.statistics.ChargeStatisticsSearch;
import com.rc.saas.tenant.service.enterprise.EnterpriseMemberService;
import com.rc.saas.tenant.service.enterprise.EnterpriseSubmemberService;
import com.rc.saas.tenant.service.hlht.HlhtOperatorService;
import com.rc.saas.tenant.service.operator.TenantOperatorStationService;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.service.tenant.TenantUserService;
import com.rc.saas.tenant.vo.elec.ChargeLeaderboard;
import com.rc.saas.tenant.vo.gun.ElecGunMonitorChartVo;
import com.rc.saas.tenant.vo.log.ChargedProcessLog;
import com.rc.saas.tenant.vo.order.OrderAllDataVo;
import com.rc.saas.tenant.vo.order.OrderPrintVo;
import com.rc.saas.tenant.vo.order.OrderResult;
import com.rc.saas.tenant.vo.order.OrderTotalData;
import com.rc.saas.tenant.vo.statistics.ChargingStatisticsVo;
import com.rc.saas.tenant.vo.statistics.OperatorPowerTimePeriodVo;
import com.rc.saas.tenant.vo.statistics.OrderChargingStatisticsVo;
import com.rc.saas.tenant.vo.statistics.StatisticsQueryVo;
import com.rc.saas.tenant.vo.order.*;
import com.rc.saas.tenant.vo.tenant.TenantUserRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by sven on 2018-04-17
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private RedisLoader redisLoader;

    @Autowired
    private EnterpriseSubmemberService enterpriseSubmemberService;

    @Autowired
    private TenantRoleService tenantRoleService;

    @Autowired
    private EnterpriseMemberService enterpriseMemberService;

    @Autowired
    @Lazy
    private TenantOperatorStationService tenantOperatorStationService;

    @Autowired
    private HlhtOperatorService hlhtOperatorService;

    @Autowired
    private TenantOperatorMapper tenantOperatorMapper;

    @Override
    public OrderInfoMapper getMapper() {
        return orderInfoMapper;
    }

    @Override
    public Long insertReturnId(OrderInfo orderInfo) {
        return orderInfoMapper.insertReturnId(orderInfo);
    }

//    @Override
//    public void updateOrderByMongDb(ChargedProcessLog processLog) {
//        OrderInfoExample orderInfoExample = new OrderInfoExample();
//        orderInfoExample.createCriteria().andOrderNumEqualTo(processLog.getOrderNum());
//
//        List<OrderInfo> orderInfos = orderInfoMapper.selectByExample(orderInfoExample);
//        OrderInfo orderInfo = orderInfos.get(0);
//
//        BigDecimal elecAmount = new BigDecimal(processLog.getElecAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
//        BigDecimal serviceAmount = new BigDecimal(processLog.getServiceAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
//        BigDecimal power = new BigDecimal(processLog.getServiceAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
//        BigDecimal totalAmount = elecAmount.add(serviceAmount);
//        Date date = DateHelper.stringToDate(processLog.getCreateTime(), DateHelper.DATE_COMMON_FORMAT);
//
//        orderInfo.setTotalElecMoney(elecAmount);
//        orderInfo.setTotalSeviceMoney(serviceAmount);
//        orderInfo.setTotalMoney(totalAmount);
//        orderInfo.setPayAmount(totalAmount);
//        orderInfo.setPileStopReason(1);
//        orderInfo.setTotalPower(power);
//
//        if (orderInfo.getEndTime() == null) {
//            orderInfo.setEndTime(date);
//        }
//
//        orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
//    }

    @Override
    public OrderInfo selectOrderInfoByMigrateId(String tenantCode, String hlhtOrderType, Long migrateId) {
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtOrderTypeEqualTo(hlhtOrderType).andMigrateIdEqualTo(migrateId);
        List<OrderInfo> list = orderInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public OrderAllDataVo selectTenantOrderByDay(String tenantCode, String dateType, OrderAllDataVo orderAllDataVo) {
        OrderAllDataVo newOrderAllDataVo = null;
        if (orderAllDataVo == null) {
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDay(tenantCode, dateType);
            if (newOrderAllDataVo == null) {
                return null;
            }
        }
        if ("1".equals(dateType)) {
            if (orderAllDataVo == null) {
                if (dataIsNull(newOrderAllDataVo)) return newOrderAllDataVo;
                return newOrderAllDataVo;
            }
            return orderAllDataVo;
        }


        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        //今天
        String today = dateFormat.format(calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        //昨天
        String yesterday = dateFormat.format(calendar.getTime());

        String lastOrderData = (String) redisLoader.get(tenantCode + dateType);
        //是否第一次登陆
        if (lastOrderData == null) {
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDay(tenantCode, dateType);
            if (newOrderAllDataVo == null) {
                return null;
            }
            if (dataIsNull(newOrderAllDataVo)) return newOrderAllDataVo;

            OrderAllDataVo temporaryData = new OrderAllDataVo();
            //一份存放redis 另一个进行展示
            BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
            temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
            temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
            temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
            temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
            temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

            temporaryData.setLastDate(yesterday);
            String orderJson = JSON.toJSONString(temporaryData);
            redisLoader.set(tenantCode + dateType, orderJson);
            return newOrderAllDataVo;

        } else {
            OrderAllDataVo oldOrderAllDataVo = JSONObject.parseObject(lastOrderData, OrderAllDataVo.class);
            //今天已经登录过直接 返回出去
            if (oldOrderAllDataVo.getLastDate().equals(yesterday)) {
                oldOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(orderAllDataVo.getOrderCount()));
                oldOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(orderAllDataVo.getChargingTimeCount()));
                oldOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(orderAllDataVo.getChargeCount()));
                oldOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(orderAllDataVo.getServiceMoneyCount()));
                oldOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(orderAllDataVo.getTotalMoney()));
                return oldOrderAllDataVo;
            }
            //判断是否月份不一样
            if (!(today.substring(5, 7).equals(oldOrderAllDataVo.getLastDate().substring(5, 7)))) {

                if ("2".equals(dateType)) {
                    if ("01".equals(today.substring(8, 10))) {
                        return orderAllDataVo;
                    }
                    //不是本月一号查询本年数据
                    newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDay(tenantCode, dateType);

                    OrderAllDataVo temporaryData = new OrderAllDataVo();
                    //一份存放redis 另一个进行展示
                    BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
                    temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
                    temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
                    temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
                    temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
                    temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

                    temporaryData.setLastDate(yesterday);
                    String orderJson = JSON.toJSONString(temporaryData);
                    redisLoader.set(tenantCode + dateType, orderJson);
                    return newOrderAllDataVo;


                } else if ("3".equals(dateType)) {

                    if (!today.substring(0, 4).equals(oldOrderAllDataVo.getLastDate().substring(0, 4))) {
                        if ("01-01".equals(today.substring(5, today.length()))) {
                            if (orderAllDataVo != null) {
                                return orderAllDataVo;
                            }
                            return newOrderAllDataVo;
                        }
                        //不是一月一号查询本年数据
                        newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDay(tenantCode, dateType);

                        OrderAllDataVo temporaryData = new OrderAllDataVo();
                        //一份存放redis 另一个进行展示
                        BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
                        temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
                        temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
                        temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
                        temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
                        temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

                        temporaryData.setLastDate(yesterday);
                        String orderJson = JSON.toJSONString(temporaryData);
                        redisLoader.set(tenantCode + dateType, orderJson);
                        return newOrderAllDataVo;
                    }

                }
            }

            //查上次到昨天的订单统计
            String lastDate = oldOrderAllDataVo.getLastDate();
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderBylastDate(tenantCode, lastDate);

            //上次登录查询出的订单加上中间未登录的订单
            newOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(newOrderAllDataVo.getOrderCount()));
            newOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(newOrderAllDataVo.getChargingTimeCount()));
            newOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(newOrderAllDataVo.getChargeCount()));
            newOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(newOrderAllDataVo.getServiceMoneyCount()));
            newOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(newOrderAllDataVo.getTotalMoney()));
            newOrderAllDataVo.setLastDate(yesterday);

            //放入缓存中
            String orderJson = JSON.toJSONString(newOrderAllDataVo);
            redisLoader.set(tenantCode + dateType, orderJson);

            //加上今天的数据
            newOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(orderAllDataVo.getOrderCount()));
            newOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(orderAllDataVo.getChargingTimeCount()));
            newOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(orderAllDataVo.getChargeCount()));
            newOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(orderAllDataVo.getServiceMoneyCount()));
            newOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(orderAllDataVo.getTotalMoney()));

            return newOrderAllDataVo;
        }
    }

    private boolean dataIsNull(OrderAllDataVo newOrderAllDataVo) {
        if (new BigDecimal(0).equals(newOrderAllDataVo.getOrderCount())) {
            newOrderAllDataVo.setTotalMoney(new BigDecimal(0));
            newOrderAllDataVo.setChargingTimeCount(new BigDecimal(0));
            newOrderAllDataVo.setOrderCount(new BigDecimal(0));
            newOrderAllDataVo.setServiceMoneyCount(new BigDecimal(0));
            newOrderAllDataVo.setChargeCount(new BigDecimal(0));
            return true;
        }
        return false;
    }

    @Override
    public List<ChargeLeaderboard> listStationChargeLeaderboard(String tenantCode) {
        List<ChargeLeaderboard> elecStations = orderInfoMapper.listStationChargeLeaderboard(tenantCode);
        return elecStations;
    }

    @Override
    public List<ChargeLeaderboard> listStationChargeLeaderboardOperator(String tenantOperatorCode) {
        List<ChargeLeaderboard> elecStations = orderInfoMapper.listStationChargeLeaderboardOperator(tenantOperatorCode);
        return elecStations;
    }

    @Override
    public OrderAllDataVo selectTenantOrderByDayOperator(String tenantOperatorCode, String dateType, OrderAllDataVo orderAllDataVo) {
        OrderAllDataVo newOrderAllDataVo = null;
        if (orderAllDataVo == null) {
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDayOperator(tenantOperatorCode, dateType);
            if (newOrderAllDataVo == null) {
                return null;
            }
        }
        if ("1".equals(dateType)) {
            if (orderAllDataVo == null) {
                if (dataIsNull(newOrderAllDataVo)) return newOrderAllDataVo;
                return newOrderAllDataVo;
            }
            return orderAllDataVo;
        }


        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        //今天
        String today = dateFormat.format(calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        //昨天
        String yesterday = dateFormat.format(calendar.getTime());

        String lastOrderData = (String) redisLoader.get(tenantOperatorCode + dateType);
        //是否第一次登陆
        if (lastOrderData == null) {
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDayOperator(tenantOperatorCode, dateType);
            if (newOrderAllDataVo == null) {
                return null;
            }
            if (dataIsNull(newOrderAllDataVo)) return newOrderAllDataVo;

            OrderAllDataVo temporaryData = new OrderAllDataVo();
            //一份存放redis 另一个进行展示
            BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
            temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
            temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
            temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
            temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
            temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

            temporaryData.setLastDate(yesterday);
            String orderJson = JSON.toJSONString(temporaryData);
            redisLoader.set(tenantOperatorCode + dateType, orderJson);
            return newOrderAllDataVo;

        } else {
            OrderAllDataVo oldOrderAllDataVo = JSONObject.parseObject(lastOrderData, OrderAllDataVo.class);
            //今天已经登录过直接 返回出去
            if (oldOrderAllDataVo.getLastDate().equals(yesterday)) {
                oldOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(orderAllDataVo.getOrderCount()));
                oldOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(orderAllDataVo.getChargingTimeCount()));
                oldOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(orderAllDataVo.getChargeCount()));
                oldOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(orderAllDataVo.getServiceMoneyCount()));
                oldOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(orderAllDataVo.getTotalMoney()));
                return oldOrderAllDataVo;
            }
            //判断是否月份不一样
            if (!(today.substring(5, 7).equals(oldOrderAllDataVo.getLastDate().substring(5, 7)))) {

                if ("2".equals(dateType)) {
                    if ("01".equals(today.substring(8, 10))) {
                        return orderAllDataVo;
                    }
                    //不是本月一号查询本年数据
                    newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDayOperator(tenantOperatorCode, dateType);

                    OrderAllDataVo temporaryData = new OrderAllDataVo();
                    //一份存放redis 另一个进行展示
                    BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
                    temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
                    temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
                    temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
                    temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
                    temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

                    temporaryData.setLastDate(yesterday);
                    String orderJson = JSON.toJSONString(temporaryData);
                    redisLoader.set(tenantOperatorCode + dateType, orderJson);
                    return newOrderAllDataVo;


                } else if ("3".equals(dateType)) {

                    if (!today.substring(0, 4).equals(oldOrderAllDataVo.getLastDate().substring(0, 4))) {
                        if ("01-01".equals(today.substring(5, today.length()))) {
                            if (orderAllDataVo != null) {
                                return orderAllDataVo;
                            }
                            return newOrderAllDataVo;
                        }
                        //不是一月一号查询本年数据
                        newOrderAllDataVo = orderInfoMapper.selectTenantOrderByDayOperator(tenantOperatorCode, dateType);

                        OrderAllDataVo temporaryData = new OrderAllDataVo();
                        //一份存放redis 另一个进行展示
                        BeanUtils.copyProperties(newOrderAllDataVo, temporaryData);
                        temporaryData.setOrderCount(newOrderAllDataVo.getOrderCount().subtract(orderAllDataVo.getOrderCount()));
                        temporaryData.setChargingTimeCount(newOrderAllDataVo.getChargingTimeCount().subtract(orderAllDataVo.getChargingTimeCount()));
                        temporaryData.setChargeCount(newOrderAllDataVo.getChargeCount().subtract(orderAllDataVo.getChargeCount()));
                        temporaryData.setServiceMoneyCount(newOrderAllDataVo.getServiceMoneyCount().subtract(orderAllDataVo.getServiceMoneyCount()));
                        temporaryData.setTotalMoney(newOrderAllDataVo.getTotalMoney().subtract(orderAllDataVo.getTotalMoney()));

                        temporaryData.setLastDate(yesterday);
                        String orderJson = JSON.toJSONString(temporaryData);
                        redisLoader.set(tenantOperatorCode + dateType, orderJson);
                        return newOrderAllDataVo;
                    }

                }
            }

            //查上次到昨天的订单统计
            String lastDate = oldOrderAllDataVo.getLastDate();
            newOrderAllDataVo = orderInfoMapper.selectTenantOrderBylastDateOperator(tenantOperatorCode, lastDate);

            //上次登录查询出的订单加上中间未登录的订单
            newOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(newOrderAllDataVo.getOrderCount()));
            newOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(newOrderAllDataVo.getChargingTimeCount()));
            newOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(newOrderAllDataVo.getChargeCount()));
            newOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(newOrderAllDataVo.getServiceMoneyCount()));
            newOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(newOrderAllDataVo.getTotalMoney()));
            newOrderAllDataVo.setLastDate(yesterday);

            //放入缓存中
            String orderJson = JSON.toJSONString(newOrderAllDataVo);
            redisLoader.set(tenantOperatorCode + dateType, orderJson);

            //加上今天的数据
            newOrderAllDataVo.setOrderCount(oldOrderAllDataVo.getOrderCount().add(orderAllDataVo.getOrderCount()));
            newOrderAllDataVo.setChargingTimeCount(oldOrderAllDataVo.getChargingTimeCount().add(orderAllDataVo.getChargingTimeCount()));
            newOrderAllDataVo.setChargeCount(oldOrderAllDataVo.getChargeCount().add(orderAllDataVo.getChargeCount()));
            newOrderAllDataVo.setServiceMoneyCount(oldOrderAllDataVo.getServiceMoneyCount().add(orderAllDataVo.getServiceMoneyCount()));
            newOrderAllDataVo.setTotalMoney(oldOrderAllDataVo.getTotalMoney().add(orderAllDataVo.getTotalMoney()));

            return newOrderAllDataVo;
        }
    }
/*
    @Override
    public PageInfo<OrderTotalData> listPageTotalData1(OrderTotalDataSearch orderTotalDataSearchVo) {
        PageHelper.startPage(orderTotalDataSearchVo.getOffset(), orderTotalDataSearchVo.getLimit());
        List<OrderTotalData> orderTotalDatasVos = orderInfoMapper.listTotalData1(orderTotalDataSearchVo);
        return new PageInfo<OrderTotalData>(orderTotalDatasVos);
    }*/


    @Override
    public PageInfo<OrderResult> listPageEnterpriseOrder(EnterpriseOrderSearch enterpriseOrderSearch) {
        //更改为查询订单中的enterpriseId
        // EnterpriseSubmemberExample example = new EnterpriseSubmemberExample();
        // example.createCriteria().andBelongEnterpriseIdEqualTo(enterpriseOrderSearch.getEnterpriseId());
        // List<EnterpriseSubmember> enterpriseSubmembers = enterpriseSubmemberService.selectByExample(example);
        // EnterpriseSubmember enterpriseSubmember = new EnterpriseSubmember();
        // enterpriseSubmember.setMemberId(enterpriseOrderSearch.getMemberId());
        // enterpriseSubmembers.add(enterpriseSubmember);
        PageHelper.startPage(enterpriseOrderSearch.getOffset(), enterpriseOrderSearch.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listEnterpriseOrder(enterpriseOrderSearch);
        return new PageInfo<OrderResult>(orderList);
    }

    @Override
    public List<OrderPrintVo> exportEnterpriseOrder(EnterpriseOrderSearch enterpriseOrderSearch) {
//        EnterpriseSubmemberExample example = new EnterpriseSubmemberExample();
//        example.createCriteria().andBelongEnterpriseIdEqualTo(enterpriseOrderSearch.getEnterpriseId());
//        List<EnterpriseSubmember> enterpriseSubmembers = enterpriseSubmemberService.selectByExample(example);
//        EnterpriseSubmember enterpriseSubmember = new EnterpriseSubmember();
//        enterpriseSubmember.setMemberId(enterpriseOrderSearch.getMemberId());
//        enterpriseSubmembers.add(enterpriseSubmember);
        List<OrderPrintVo> orderList = orderInfoMapper.exportEnterpriseOrder(enterpriseOrderSearch);
        return orderList;
    }

    @Override
    public PageInfo<OrderResult> listPageByCondition(OrderSearch orderSearchVo) {
        TenantUserRoleVo userRoleVo = tenantRoleService.getLonginRole(orderSearchVo.getLoginName());
        if (userRoleVo!=null){
            dealDataAuth(orderSearchVo,userRoleVo);
        }
        PageHelper.startPage(orderSearchVo.getOffset(), orderSearchVo.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listByCondition(orderSearchVo);
        return new PageInfo<OrderResult>(orderList);
    }

    public void dealDataAuth(OrderSearch orderSearchVo,TenantUserRoleVo userRoleVo){
        orderSearchVo.setRoleType(userRoleVo.getRoleType());
        orderSearchVo.setDataAuth(userRoleVo.getDataAuth());
        switch (userRoleVo.getRoleType()){
            case "1"://自定义用户
                orderSearchVo.setOperatorIds(
                        StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                                Arrays.asList(userRoleVo.getOperatorIds().split(","))
                                :new ArrayList<String>());
                orderSearchVo.setEnterpriseIds(
                        StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                                Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                                :new ArrayList<String>());
                break;
            case "2"://加盟商用户
                orderSearchVo.setOperatorIds(StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                        Arrays.asList(userRoleVo.getOperatorIds().split(","))
                        :new ArrayList<String>());
                break;
            case "3"://企业用户
                orderSearchVo.setEnterpriseIds(StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                        Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                        :new ArrayList<String>());
                break;
        }
    }

    @Override
    public List<OrderResult> listByCondition(OrderSearch orderSearchVo) {
        return orderInfoMapper.listByCondition(orderSearchVo);
    }

    @Override
    public PageInfo<OrderResult> listPageHlhtOrderInfo(HlhtOrderInfoSearch hlhtOrderInfoSearch) {
        PageHelper.startPage(hlhtOrderInfoSearch.getOffset(), hlhtOrderInfoSearch.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listByHlhtCondition(hlhtOrderInfoSearch);
        return new PageInfo<OrderResult>(orderList);
    }

    @Override
    public PageInfo<OrderResult> listByExceptionOrderCondition(ExceptionOrderInfoSearch exceptionOrderInfoSearch) {
        PageHelper.startPage(exceptionOrderInfoSearch.getOffset(), exceptionOrderInfoSearch.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listByExceptionOrderCondition(exceptionOrderInfoSearch);
        for (OrderResult orderResult : orderList) {
            if (BeanUtil.isValid(orderResult.getTenantOperatorCode())) {
                List<String> tenantOperatorCode = Arrays.asList(orderResult.getTenantOperatorCode().split(","));
                TenantOperatorExample tenantOperatorExample = new TenantOperatorExample();
                tenantOperatorExample.createCriteria().andTenantOperatorCodeIn(tenantOperatorCode);
                List<String> operatorNames = tenantOperatorMapper.selectByExample(tenantOperatorExample).stream().map(tenantOperator -> tenantOperator.getOperatorName()).distinct().collect(Collectors.toList());
                String operatorName = StringUtils.strip(operatorNames.toString(), "[]").replace(" ", "");
                orderResult.setOperatorName(operatorName);
            }
        }
        return new PageInfo<OrderResult>(orderList);
    }

    /*@Override
    public List<OrderPrintVo> exportOrderInfo(OrderSearch orderSearch) {
        List<OrderPrintVo> orderPrintVos = orderInfoMapper.exportOrderInfo(orderSearch);
        return orderPrintVos;
    }

    @Override
    public List<OrderTotalDataPrintVo> exportTotalData(OrderTotalDataSearch orderTotalDataSearchVo) {
        List<OrderTotalDataPrintVo> orderTotalDataList = orderInfoMapper.exportTotalData(orderTotalDataSearchVo);
        return orderTotalDataList;
    }

    @Override
    public List<OrderPrintVo> listOperatorOrderInfo(OrderSearch orderSearch) {
        List<OrderPrintVo> orderPrintVos = orderInfoMapper.exportOperatorOrderInfo(orderSearch);
        return orderPrintVos;
    }
*/
    @Override
    public List<ChargingStatisticsVo> listChargingStatistics(StatisticsQueryVo statisticsQueryVo) {
        return orderInfoMapper.listChargingStatistics(statisticsQueryVo);
    }

   /* @Override
    public PageInfo<OrderResult> listPageOperatorOrderInfo(OrderSearch orderSearch) {
        PageHelper.startPage(orderSearch.getOffset(), orderSearch.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listOperatorOrderInfo(orderSearch);
        return new PageInfo<OrderResult>(orderList);
    }
*/
    /*@Override
    public OrderInfoCountVo countOrderInfo(OrderInfoStatisticsSearch orderInfoStatisticsSearch) {
        String tenantCode = orderInfoStatisticsSearch.getTenantCode();
        //       List<Long> memberIdList = new ArrayList<>();

        HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(orderInfoStatisticsSearch.getHlhtTenantOrHlhtOperator());
        if (hlhtOperator != null) {
            if ("0".equals(hlhtOperator.getOperatorType())) {
                orderInfoStatisticsSearch.setHlhtOpenType("1");
            } else {
                orderInfoStatisticsSearch.setHlhtOpenType("2");
            }
        } else if (BeanUtil.isValid(orderInfoStatisticsSearch.getHlhtTenantOrHlhtOperator())) {
            orderInfoStatisticsSearch.setHlhtOpenType("0");
        }

        if (StringUtil.isNotBlank(orderInfoStatisticsSearch.getEnterpriseMemberMobile())) {
            EnterpriseMemberExample enterpriseMemberExample = new EnterpriseMemberExample();
            enterpriseMemberExample.createCriteria().andTenantCodeEqualTo(tenantCode).andEnterpriseMobileEqualTo(orderInfoStatisticsSearch.getEnterpriseMemberMobile());
            List<EnterpriseMember> enterpriseMemberList = enterpriseMemberService.selectByExample(enterpriseMemberExample);

            if (!CollectionUtils.isEmpty(enterpriseMemberList)) {
                orderInfoStatisticsSearch.setEnterpriseId(enterpriseMemberList.get(0).getEnterpriseId());
            }
        }
//        if (BeanUtil.isValid(enterpriseMemberMobile)) {
//            //通过企业手机号找出企业主会员
//            EnterpriseMemberExample enterpriseMemberExample = new EnterpriseMemberExample();
//            enterpriseMemberExample.createCriteria().andTenantCodeEqualTo(tenantCode).andEnterpriseMobileEqualTo(enterpriseMemberMobile);
//            List<EnterpriseMember> enterpriseMemberList = enterpriseMemberService.selectByExample(enterpriseMemberExample);
//
//            if ((!CollectionUtils.isEmpty(enterpriseMemberList)) && enterpriseMemberList.size() > 0) {
//                EnterpriseMember enterpriseMember = enterpriseMemberList.get(0);
//                //设置支付类型。普通企业会员不查个人钱包，商业企业会员查所有
//                if ("0".equals(enterpriseMember.getEnterpriseType())) {
//                    orderInfoStatisticsSearch.setAccountType("0");
//                }
//                //通过企业主会员找到其子会员
//                List<Long> subMemberIdList = enterpriseSubmemberService.listMemberIdByEnterpriseId(tenantCode, enterpriseMember.getEnterpriseId());
//                if ((!CollectionUtils.isEmpty(subMemberIdList)) && subMemberIdList.size() > 0) {
//                    memberIdList.addAll(subMemberIdList);
//                }
//                memberIdList.add(enterpriseMember.getMemberId());
//            } else {
//                return new OrderInfoCountVo();
//            }
//
//        }
//        orderInfoStatisticsSearch.setMemberIdList(memberIdList);
        //分销商
        if (BeanUtil.isValid(orderInfoStatisticsSearch.getTenantOperatorCode())) {
            List<Long> elecStationIdList = tenantOperatorStationService.listElecStationIdByTenantOperator(tenantCode, orderInfoStatisticsSearch.getTenantOperatorCode());
            if (CollectionUtils.isEmpty(elecStationIdList) || elecStationIdList.size() < 0) {
                return new OrderInfoCountVo();
            }
            orderInfoStatisticsSearch.setElecStationIdList(elecStationIdList);
        }
        OrderInfoCountVo orderInfoCountVo = orderInfoMapper.countOrderInfo(orderInfoStatisticsSearch);
        if (BeanUtil.isValid(orderInfoCountVo.getTimeDiff())) {
            orderInfoCountVo.setTimeDiff(orderInfoCountVo.getTimeDiff().divide(new BigDecimal(3600), 1, BigDecimal.ROUND_HALF_UP));
        }
        if (orderInfoCountVo.getTimeDiff() == null) {
            orderInfoCountVo.setTimeDiff(new BigDecimal(0));
        }
        return orderInfoCountVo;
    }*/
/*
    @Override
    public List<OrderInfoCountPrintVo> listCountOrderInfo(OrderInfoStatisticsSearch orderInfoStatisticsSearch) {
        String enterpriseMemberMobile = orderInfoStatisticsSearch.getEnterpriseMemberMobile();
        String tenantCode = orderInfoStatisticsSearch.getTenantCode();
        HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(orderInfoStatisticsSearch.getHlhtTenantOrHlhtOperator());
        if (hlhtOperator != null) {
            if ("0".equals(hlhtOperator.getOperatorType())) {
                orderInfoStatisticsSearch.setHlhtOpenType("1");
            } else {
                orderInfoStatisticsSearch.setHlhtOpenType("2");
            }
        } else if (BeanUtil.isValid(orderInfoStatisticsSearch.getHlhtTenantOrHlhtOperator())) {
            orderInfoStatisticsSearch.setHlhtOpenType("0");
        }
        // List<Long> memberIdList = new ArrayList<>();
        if (StringUtil.isNotBlank(orderInfoStatisticsSearch.getEnterpriseMemberMobile())) {
            EnterpriseMemberExample enterpriseMemberExample = new EnterpriseMemberExample();
            enterpriseMemberExample.createCriteria().andTenantCodeEqualTo(tenantCode).andEnterpriseMobileEqualTo(orderInfoStatisticsSearch.getEnterpriseMemberMobile());
            List<EnterpriseMember> enterpriseMemberList = enterpriseMemberService.selectByExample(enterpriseMemberExample);

            if (!CollectionUtils.isEmpty(enterpriseMemberList)) {
                orderInfoStatisticsSearch.setEnterpriseId(enterpriseMemberList.get(0).getEnterpriseId());
            }
        }

//        if (BeanUtil.isValid(enterpriseMemberMobile)) {
//            //通过企业手机号找出企业主会员
//            EnterpriseMemberExample enterpriseMemberExample = new EnterpriseMemberExample();
//            enterpriseMemberExample.createCriteria().andTenantCodeEqualTo(tenantCode).andEnterpriseMobileEqualTo(enterpriseMemberMobile);
//            List<EnterpriseMember> enterpriseMemberList = enterpriseMemberService.selectByExample(enterpriseMemberExample);
//
//            if ((!CollectionUtils.isEmpty(enterpriseMemberList)) && enterpriseMemberList.size() > 0) {
//                EnterpriseMember enterpriseMember = enterpriseMemberList.get(0);
//                //设置支付类型。普通企业会员不查个人钱包，商业企业会员查所有
//                if ("0".equals(enterpriseMember.getEnterpriseType())) {
//                    orderInfoStatisticsSearch.setAccountType("0");
//                }
//                //通过企业主会员找到其子会员
//                List<Long> subMemberIdList = enterpriseSubmemberService.listMemberIdByEnterpriseId(tenantCode, enterpriseMember.getEnterpriseId());
//                if ((!CollectionUtils.isEmpty(subMemberIdList)) && subMemberIdList.size() > 0) {
//                    memberIdList.addAll(subMemberIdList);
//                }
//                memberIdList.add(enterpriseMember.getMemberId());
//            } else {
//                return null;
//            }
//
//        }
        //       orderInfoStatisticsSearch.setMemberIdList(memberIdList);
        //分销商
        if (BeanUtil.isValid(orderInfoStatisticsSearch.getTenantOperatorCode())) {
            List<Long> elecStationIdList = tenantOperatorStationService.listElecStationIdByTenantOperator(tenantCode, orderInfoStatisticsSearch.getTenantOperatorCode());
            if (CollectionUtils.isEmpty(elecStationIdList) || elecStationIdList.size() < 0) {
                return null;
            }
            orderInfoStatisticsSearch.setElecStationIdList(elecStationIdList);
        }
        List<OrderInfoCountPrintVo> orderInfoCountPrintVoList = orderInfoMapper.listCountOrderInfo(orderInfoStatisticsSearch);
        return orderInfoCountPrintVoList;
    }*/


    @Override
    public PageInfo<OrderChargingStatisticsVo> listOrderChargingStatistics(StatisticsQueryVo statisticsQueryVo) {
        PageHelper.startPage(statisticsQueryVo.getOffset(), statisticsQueryVo.getLimit());
        List<OrderChargingStatisticsVo> orderChargingStatisticVOS = orderInfoMapper.listOrderChargingStatistics(statisticsQueryVo);
        return new PageInfo<OrderChargingStatisticsVo>(orderChargingStatisticVOS);
    }

    @Override
    public OrderResult selectOrderInfo(Long orderId) {
        return orderInfoMapper.selectOrderInfo(orderId);
    }

    @Override
    public Map<String,Object> getStatementData(String stationName,String provinceId,String cityId,String areaId,String bizType) {
        return orderInfoMapper.getStatementData(stationName,provinceId,cityId,areaId,bizType);
    }

    @Override
    public List<OperatorPowerTimePeriodVo> listOperatorPowerTimePeriod(List<ElecStationPrice> list, StatisticsQueryVo statisticsQueryVo) {
        String begin = statisticsQueryVo.getStartTime();
        String end = statisticsQueryVo.getEndTime();

        if (StringUtil.isBlank(statisticsQueryVo.getEndTime())) {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            end = format.format(date);
        } else {
            end += " 23:59:59";
        }

        ArrayList<OperatorPowerTimePeriodVo> arrayList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            ElecStationPrice price = list.get(i);

            String bsBegin = DateHelper.dataToString(price.getBeginTime(), DateHelper.DATE_FORMAT_HHMMSS);
            String bsEnd = DateHelper.dataToString(price.getEndTime(), DateHelper.DATE_FORMAT_HHMMSS);

            if (bsEnd.equals("00:00:00")) {
                bsEnd = "23:59:59";
            }

            String bsBeginHHmm = DateHelper.dataToString(price.getBeginTime(), DateHelper.DATE_FORMAT_HHMM);
            String bsEndHHmm = DateHelper.dataToString(price.getEndTime(), DateHelper.DATE_FORMAT_HHMM);

            String priceCode = price.getPriceCode();

            if ("1".equals(priceCode)) {
                priceCode = "尖峰";
            } else if ("2".equals(priceCode)) {
                priceCode = "高峰";
            } else if ("3".equals(priceCode)) {
                priceCode = "平段";
            } else if ("4".equals(priceCode)) {
                priceCode = "低谷";
            }

            bsBeginHHmm = priceCode + " " + bsBeginHHmm + "~" + bsEndHHmm;

            OperatorPowerTimePeriodVo operatorPowerTimePeriodVo = orderInfoMapper.selectOperatorPowerTimePeriod(statisticsQueryVo.getElecStationId(), begin, end, bsBegin, bsEnd, bsBeginHHmm);
            arrayList.add(operatorPowerTimePeriodVo);
        }
        return arrayList;
    }

    @Override
    public long countOrderIsCharging(Long memberId) {
        List<String> status = new ArrayList<String>() {
            {
                this.add("1");
                this.add("3");
            }
        };
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andStatusIn(status);
        return orderInfoMapper.countByExample(example);
    }

    @Override
    public OrderInfo selectOrderInfoByOutOrderNum(String outOrderNum) {
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria().andOutOrderNumEqualTo(outOrderNum);
        List<OrderInfo> list = orderInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }


    //@Override
    //public OrderInfo selectOrderInfoByMigrateId(String tenantCode, Long migrateId) {
    //    OrderInfoExample example = new OrderInfoExample();
    //    example.createCriteria().andTenantCodeEqualTo(tenantCode).andMigrateIdEqualTo(migrateId);
    //    List<OrderInfo> list = orderInfoMapper.selectByExample(example);
    //    if (!CollectionUtils.isEmpty(list)) {
    //        return list.get(0);
    //    }
    //    return null;
    //}

    @Override
    public PageInfo<OrderInfo> listhlht1PageOrder(Long memberId, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(orderInfoMapper.listhlht1PageOrder(memberId));
    }

    //@Override
    //public PageInfo<OrderInfo> listOrderInfoByYear(Integer year, int pageIndex, int pageSize) {
    //    PageHelper.startPage(pageIndex, pageSize);
    //    return new PageInfo<>(orderInfoMapper.listOrderInfoByYear(year));
    //}

    //@Override
    //public PageInfo<OrderInfo> listOrderInfoByDay(Date createdTime, int pageIndex, int pageSize) {
    //    PageHelper.startPage(pageIndex, pageSize);
    //    return new PageInfo<>(orderInfoMapper.listOrderInfoByDay(createdTime));
    //}


    @Override
    public OrderInfo selectOrderByOrderNum(String orderNum) {
        return orderInfoMapper.selectOrderByOrderNum(orderNum);
    }
/*

    @Override
    public AcctRuleOrderResult AcctRuleOrder(String TenantCode, Long elecStationId, String startTime, String endTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String invoiceStartTime = new SimpleDateFormat("yyyyMMddHHmmss").format(format.parse(startTime));
        String invoiceEndTime = new SimpleDateFormat("yyyyMMddHHmmss").format(format.parse(endTime));
        return orderInfoMapper.AcctRuleOrder(TenantCode, elecStationId, startTime, endTime,invoiceStartTime,invoiceEndTime);
    }
*/

    @Override
    public List<ExceptionOrderPrintVo> exportExceptionOrderInfo(ExceptionOrderInfoSearch orderSearch) {
        List<ExceptionOrderPrintVo> exceptionOrderPrintVos = orderInfoMapper.exportExceptionOrderInfo(orderSearch);
        for (ExceptionOrderPrintVo exceptionOrderPrintVo : exceptionOrderPrintVos) {
            if (BeanUtil.isValid(exceptionOrderPrintVo.getTenantOperatorCode())) {
                List<String> tenantOperatorCode = Arrays.asList(exceptionOrderPrintVo.getTenantOperatorCode().split(","));
                TenantOperatorExample tenantOperatorExample = new TenantOperatorExample();
                tenantOperatorExample.createCriteria().andTenantOperatorCodeIn(tenantOperatorCode);
                List<String> operatorNames = tenantOperatorMapper.selectByExample(tenantOperatorExample).stream().map(tenantOperator -> tenantOperator.getOperatorName()).distinct().collect(Collectors.toList());
                String operatorName = StringUtils.strip(operatorNames.toString(), "[]").replace(" ", "");
                exceptionOrderPrintVo.setOperatorName(operatorName);
            }
        }
        return exceptionOrderPrintVos;
    }

    @Override
    public PageInfo<OrderResult> listPageOperatorExceptionOrderInfo(ExceptionOrderInfoSearch orderSearch) {
        PageHelper.startPage(orderSearch.getOffset(), orderSearch.getLimit());
        List<OrderResult> orderList = orderInfoMapper.listOperatorExceptionOrderInfo(orderSearch);
        return new PageInfo<OrderResult>(orderList);
    }

    @Override
    public List<HlhtOrderInfoPrintVo> exportHlhtOrderInfo(HlhtOrderInfoSearch hlhtOrderInfoSearch) {
        List<HlhtOrderInfoPrintVo> orderPrintVos = orderInfoMapper.exportHlhtOrderInfo(hlhtOrderInfoSearch);
        return orderPrintVos;
    }

    @Override
    public PageInfo<SaasOrderTotalData> listPageSaasTotalData1(OrderTotalDataSearch orderTotalDataSearchVo) {
        PageHelper.startPage(orderTotalDataSearchVo.getOffset(), orderTotalDataSearchVo.getLimit());
        List<SaasOrderTotalData> orderTotalDatasVos = orderInfoMapper.listSaasTotalData1(orderTotalDataSearchVo);
        return new PageInfo<SaasOrderTotalData>(orderTotalDatasVos);
    }

    @Override
    public List<SaasOrderTotalDataPrintVo> exportSaasTotalData(OrderTotalDataSearch orderTotalDataSearchVo) {
        List<SaasOrderTotalDataPrintVo> orderTotalDataList = orderInfoMapper.exportSaasTotalData(orderTotalDataSearchVo);
        return orderTotalDataList;
    }

    @Override
    public SaasOrderInfoCountVo saasCountOrderInfo(OrderTotalDataSearch orderTotalDataSearch) {
        SaasOrderInfoCountVo saasOrderInfoCountVo = orderInfoMapper.saasCountOrderInfo(orderTotalDataSearch);
        return saasOrderInfoCountVo;
    }

    @Override
    public List<SaasOrderInfoCountPrintVo> listSaasCountOrderInfo(OrderTotalDataSearch orderTotalDataSearch) {
        List<SaasOrderInfoCountPrintVo> saasOrderInfoCountPrintVos = orderInfoMapper.listSaasCountOrderInfo(orderTotalDataSearch);
        return saasOrderInfoCountPrintVos;
    }

    @Override
    public HlhtOrderInfo selectHlhtOrderInfoByOrderId(Long orderId) {
        return orderInfoMapper.selectHlhtOrderInfoByOrderId(orderId);
    }

    @Override
    public List<ExceptionOrderPrintVo> exportOperatorExceptionOrderInfo(ExceptionOrderInfoSearch exceptionOrderInfoSearch) {
        return orderInfoMapper.exportOperatorExceptionOrderInfo(exceptionOrderInfoSearch);
    }

    @Override
    public List<OrderResult> listOrderInIds(List<Long> ids) {
        return orderInfoMapper.listOrderInIds(ids);
    }

    @Override
    public List<ChargeStatisticsResult> listChargeStatistics(ChargeStatisticsSearch search) {
        return orderInfoMapper.listChargeStatistics(search);
    }

    @Override
    public List<OrderTraceVo> listOrderTrace(OrderTraceVo orderTraceVo) {
        List<OrderTraceVo> orderTraceVos = orderInfoMapper.listOrderTrace(orderTraceVo);
        if (orderTraceVos.size() == 0){
            return null;
        }
        for (int j = 0; j < 24; j++) {
            int i = 0;
            String hour = String.valueOf(j);
            if (hour.length() == 1) {
                hour = "0" + hour;
            }
            for (OrderTraceVo traceVo : orderTraceVos) {
                if (traceVo.getOrderHour().equals(hour)) {
                    i = 1;
                    continue;

                }
            }

            //时段不存在则新增
            if (i == 0) {
                OrderTraceVo vo = new OrderTraceVo();
                vo.setOrderHour(hour);
                vo.setTotalPower(BigDecimal.ZERO);
                orderTraceVos.add(vo);
            }
        }
        List<OrderTraceVo> sortList = orderTraceVos.stream().sorted(Comparator.comparing(OrderTraceVo::getOrderHour)).collect(Collectors.toList());

        return sortList;
    }
}
